/**
 * This file contains the implementation of the log in form for the Order App. 
 * @author Group 10
 */

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import rmiinterfaces.SecurityInterface;

// This class implements a login form for the Order App. 
public class OrderAppLogin extends JFrame implements ActionListener
{
    private JTextField userName;
    private JTextField password;
    private JButton loginButton;
    private JButton cancelButton;
    private OrderApp parent;
    
    public OrderAppLogin(OrderApp parent)
    {
        this.parent = parent;
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // specify the panel
        JPanel panel = new JPanel(new GridLayout(3,2));
	
        // create Labels
        JLabel userNameLabel = new JLabel("Username: ");
        userName = new JTextField();
        JLabel passwordLabel = new JLabel("Password: ");
        password = new JTextField();
        
        // create the buttons
        loginButton = new JButton("Login");
        loginButton.addActionListener(this);
        cancelButton = new JButton("Cancel and Close");
        cancelButton.addActionListener(this);

        // add the labels and buttons to the panel
        panel.add(userNameLabel);
        panel.add(userName);
        panel.add(passwordLabel);
        panel.add(password);
        panel.add(loginButton);
        panel.add(cancelButton);
        
        // set the panel and window sizes
        this.add(panel);
        this.pack();
        this.setResizable(false);
     
    }
    
    public void popUp()
    {
        this.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource() == loginButton)
        {   
            boolean result = false;
            try {
                result = sendLoginCredentials();
            } 
            catch (RemoteException ex) {
                ex.printStackTrace();
            }
            if(result)
            {
                parent.login(userName.getText(), password.getText());
                this.close();
            }
            else // if the credentials
            {
                JOptionPane.showMessageDialog(null, "Credentials Invalid");
                userName.setText("");
                password.setText("");
            }
        }
        else // the souce is not the Login, so it must be the cancel
        {
            // close the application
            System.exit(0);
        }
    }
    
    public boolean sendLoginCredentials() throws RemoteException
    {
        boolean retVal = true;
        SecurityInterface.AuthReturn ret = parent.getRMIServer().logIn(userName.getText(), password.getText());
        System.out.println(ret.value);
        retVal = ret.equals(SecurityInterface.AuthReturn.TRUE);
        
        return retVal;
    }
    
    public void close()
    {
        this.setVisible(false);
    }
}
